package com.ctgu.tengyun.service.sparepart.impl;

import com.ctgu.tengyun.dao.mybatis.SparePartInfoMapper;
import com.ctgu.tengyun.domain.SparePartInfo;
import com.ctgu.tengyun.domain.SparePartInfoExample;
import com.ctgu.tengyun.service.sparepart.SparePartInfoService;
import com.ctgu.tengyun.util.CollectionsUtil;
import com.ctgu.tengyun.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/7/26.
 */
@Service
public class SparePartInfoServiceImpl implements SparePartInfoService {

    @Resource
    private SparePartInfoMapper mapper;

    @Override
    public int insertSelective(SparePartInfo record) {
        return mapper.insertSelective(record);
    }

    @Override
    public SparePartInfo selectByPartNo(String partNo) {
        SparePartInfoExample example = new SparePartInfoExample();
        SparePartInfoExample.Criteria criteria = example.createCriteria();

        criteria.andPartNoEqualTo(partNo);

        List<SparePartInfo> sparePartInfoList =  mapper.selectByExample(example);
        if(CollectionsUtil.isNotEmpty(sparePartInfoList)){
            return sparePartInfoList.get(0);
        }

        return null;
    }

    @Override
    public Integer countSparePart(String productNo, String partNo) {
        SparePartInfoExample example = new SparePartInfoExample();
        SparePartInfoExample.Criteria criteria = example.createCriteria();

        criteria.andIsValidEqualTo(1);
        criteria.andPartStatusEqualTo(1); //正常状态
        if(StringUtils.isNotEmpty(productNo)){
            criteria.andProductNoEqualTo(productNo);
        }
        if(StringUtils.isNotEmpty(partNo)){
            criteria.andPartNoEqualTo(partNo);
        }

        return mapper.countByExample(example);
    }

    @Override
    public List<SparePartInfo> selectSparePartList(String productNo, String partNo, Integer pageNo, Integer pageSize) {
        SparePartInfoExample example = new SparePartInfoExample();
        SparePartInfoExample.Criteria criteria = example.createCriteria();

        criteria.andIsValidEqualTo(1);
        criteria.andPartStatusEqualTo(1); //正常状态
        if(StringUtils.isNotEmpty(productNo)){
            criteria.andProductNoEqualTo(productNo);
        }
        if(StringUtils.isNotEmpty(partNo)){
            criteria.andPartNoEqualTo(partNo);
        }

        example.setStartIndex((pageNo - 1) * pageSize);
        example.setPageSize(pageSize);

        return mapper.selectByExample(example);
    }

    @Override
    public SparePartInfo selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SparePartInfo record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public boolean reduceStock(Long id, Integer reduceCount) {
        return mapper.reduceStock(id, reduceCount) == 1;
    }
}
